<template>
  <!-- hidden PageHeaderWrapper title demo -->
  <page-header-wrapper :title="false" class="addmoban">
    <a-row type="flex" justify="space-between" align="top">
      <a-col :span="14">
        <a-card :bordered="false">
          <h3 class="addmoban-title">发证设置</h3>
          <a-form v-bind="formLayout" class="fzfrom" :form="form" @submit="handleSubmit">
            <a-form-item label="证件模板名称">
              <span class="fz-span">{{ fromData.template_name }}</span>
            </a-form-item>
            <a-form-item label="领证有效期">
              <span class="fz-span">{{ fromData.send_code_ts }}</span>
            </a-form-item>

            <a-form-item label="发证方式" required>
              <a-radio-group default-value="a" v-model="inviteRadio">
                <a-radio-button value="a"> 单独发证 </a-radio-button>
                <a-radio-button value="b" style="margin-left: 20px"> 邀请领证 </a-radio-button>
              </a-radio-group>
              <p style="color: #999999; font-size: 12px" v-show="inviteRadio == 'b'">
                无需指定发证对象，由领证人申请，后台激活证件
              </p>
            </a-form-item>
            <!-- <a-form-item label="领证码">
              <a-button type="link" block class="djsc" :loading="loading" @click="getCode" v-show="!imgShow"
                >点击生成</a-button
              >
              <a-spin v-if="ispreview != '1'" :spinning="spinning">
                <div class="imgfather" v-show="imgShow" @mouseenter="enter" @mouseleave="leave">
                  <img :src="imgUrl.url" alt="" class="imgUrl" />
                  <div class="imgzzc" v-show="seen">
                    <a-button @click="getCode" style="margin-bottom: 20px">刷新</a-button>
                    <a-button type="primary" @click="downImg(imgUrl.download)"> 保存 </a-button>
                  </div>
                </div>
              </a-spin>
            </a-form-item> -->
            <div class="aloneGiveCert" v-show="inviteRadio == 'a'">
              <h3>发证信息</h3>
              <a-form-item label="姓名">
                <a-input
                  v-decorator="['name', { rules: [{ required: true, message: '请输入姓名！' }] }]"
                  :maxLength="100"
                  placeholder="请输入内容"
                  style="width: 300px"
                />
              </a-form-item>
              <a-form-item :label="fromData.card_key_one" v-if="fromData.card_key_one">
                <a-input
                  v-decorator="['card_content_one', { rules: [{ required: true, message: '请输入内容！' }] }]"
                  :maxLength="100"
                  placeholder="请输入内容"
                  style="width: 300px"
                />
              </a-form-item>
              <a-form-item :label="fromData.card_key_two" v-if="fromData.card_key_two">
                <a-input
                  v-decorator="['card_content_two', { rules: [{ required: true, message: '请输入内容！' }] }]"
                  :maxLength="100"
                  placeholder="请输入内容"
                  style="width: 300px"
                />
              </a-form-item>
              <a-form-item :label="fromData.card_key_three" v-if="fromData.card_key_three">
                <a-input
                  v-decorator="['card_content_three', { rules: [{ required: true, message: '请输入内容！' }] }]"
                  :maxLength="100"
                  placeholder="请输入内容"
                  style="width: 300px"
                />
              </a-form-item>
              <a-form-item label="手机号码">
                <a-input
                  v-decorator="['phone', { rules: [{ required: true, message: '请输入发证对象手机号码！' }] }]"
                  :maxLength="100"
                  placeholder="请输入发证对象手机号码！"
                  style="width: 300px"
                />
              </a-form-item>
            </div>

            <a-spin v-if="ispreview != '1'" :spinning="spinning">
              <a-form-item :wrapperCol="{ span: 24 }" style="text-align: center">
                <a-button @click="handleSubmit" type="primary" class="bcbtn">确认</a-button>

                <a-button @click="handleBack" class="bcbtn">返回</a-button>
              </a-form-item>
            </a-spin>
          </a-form>
        </a-card>
      </a-col>
      <a-col :span="10">
        <certificate-style-one
          :formData="fromData"
          :qianz="fromData.seal == 1 ? true : false"
          :imgvalue="fromData.company_seal"
        />
      </a-col>
    </a-row>
    <!-- 确认按钮弹出领证码 -->
    <!-- 新增发证弹窗 -->
    <a-modal
      v-model="codeVisible"
      title="领证码"
      class="codeModal"
      :width="400"
      okText="保存"
      cancelText="关闭"
      @ok="downImg"
      @cancel="handleCancel"
    >
      <div class="codeImg">
        <p>发证提交成功！</p>

        <a-spin tip="加载中..." :spinning="codeSpinning">
          <div class="codeImg-centent">
            <img :src="imgUrl.url" alt="" @load="imgLoad" />
          </div>
        </a-spin>
        <p>请保存该领证码，以便后续邀请使用</p>
      </div>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { uplaodImg } from '@/api/common.js'
import {
  addNews,
  gettypeList,
  getRecommendClassList,
  getNewsDetail,
  updateNews,
  getTemplateInfo,
  getCodeInfo,
  getAloneCode,
} from '@/api/news'
import { validateURL } from '@/utils/validate.js'
import certificateStyleOne from '@/components/certificateStyle/certificateOne'

import moment from 'moment'
import pick from 'lodash.pick'

export default {
  name: 'NewsDetail',
  components: {
    certificateStyleOne,
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 17 },
      },
    }
    return {
      form: this.$form.createForm(this),
      isClear: false,
      source: '1',
      typeList: [],
      recommendClassList: [],
      uploadkey: Math.random(),
      spinning: false,
      id: this.$route.params.id,
      imgvalue: '',
      ispreview: this.$route.query.ispreview,
      template_id2: null,
      fromData: {},
      imgUrl: {},
      imgShow: false,
      seen: false,
      codeVisible: false,
      codeSpinning: true,
      inviteRadio: 'a',
    }
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  created() {
    // this.gettypeList()
    // console.log(this.$route.query)
    this.template_id2 = this.$route.query.id
    if (this.template_id2) {
      this.getInfo()
      this.form.resetFields()
    }
  },
  props: {
    template_id: {
      type: Number,
    },
  },
  computed: {
    isShow2() {
      return this.$store.state.WFStore.TypesIsShow2
    },
  },

  watch: {},
  watch: {
    template_id: function (v) {
      console.log(v)
      // 监听传过来的template_id变化，如果改变了去请求接口
      this.template_id2 = v
      this.imgShow = false
      if (v !== null) {
        this.getInfo()
      } else {
        this.form.resetFields()
      }
    },
    isShow2(newVal, oldVal) {
      //do something
      if (newVal) {
        this.form.resetFields()
      }
    },
  },
  methods: {
    handleBack() {
      this.$router.go(-1)
    },
    // 单独发证
    handleSubmit(e) {
      e.preventDefault()
      if (this.inviteRadio == 'a') {
        this.form.validateFields((err, values) => {
          console.log(values)
          if (!err) {
            this.imgUrl = {}
            let param = {
              template_id: parseInt(this.template_id2),
              name: values.name,
              card_content_one: values.card_content_one,
              card_content_two: values.card_content_two,
              card_content_three: values.card_content_three,
              phone: values.phone,
            }
            getAloneCode(param)
              .then((res) => {
                console.log(res)
                this.codeVisible = true
                this.codeSpinning = true
                this.imgUrl = res.data
              })
              .catch((err) => {
                console.log(err)
                if (err) {
                  this.codeVisible = false
                }
              })
          }
        })
      } else if (this.inviteRadio == 'b') {
        this.getCode()
      }
    },
    // 生成邀请领证二维码
    getCode() {
      this.codeVisible = true
      this.codeSpinning = true
      this.imgUrl = {}
      getCodeInfo({
        template_id: parseInt(this.template_id2),
      })
        .then((res) => {
          console.log(res)
          this.imgUrl = res.data
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 下载二维码
    downImg() {
      window.open(this.imgUrl.download)
      this.codeVisible = false
      this.$router.push('/idPhoto/manage')
    },
    // 二维码加载完成
    imgLoad() {
      this.codeSpinning = false
    },
    // 获取详情
    getInfo() {
      this.fromData = {}
      getTemplateInfo({
        company_id: 0,
        template_id: parseInt(this.template_id2),
      })
        .then((res) => {
          console.log(res)
          if (res.data.company_seal) {
            res.data.company_seal = window.location.protocol + '//' + window.location.host + res.data.company_seal
          }
          this.fromData = res.data
          this.codeSpinning = false
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 鼠标移入
    enter() {
      this.seen = true
    },
    // 鼠标移出
    leave() {
      this.seen = false
    },
    // 关闭弹窗返回列表
    handleCancel(e) {
      // console.log(e)
      this.$router.push('/idPhoto/manage')
    },
  },
  destroyed() {
    this.form.resetFields()
  },
}
</script>
<style lang="less" scoped>
.addmoban .ant-form-item {
  margin-bottom: 20px;
}
.addmoban-title {
  position: relative;
  font-weight: bold;
  margin-bottom: 30px;
}
.addmoban-title::after {
  position: absolute;
  left: -7px;
  top: 50%;
  margin-top: -6px;
  content: '';
  display: block;
  width: 2px;
  height: 15px;
  background: #1890ff;
}
.zjStyle {
  width: 335px;
  box-sizing: border-box;
  &-content {
    background: #ffffff;
    box-shadow: 3px 3px 7px 3px rgba(8, 8, 8, 0.1);
    border-radius: 10px;
    overflow: hidden;
    header {
      height: 22px;
      background: #0579fe;
    }
    input {
      border: none;
      font-size: 16px;
    }
    &-main {
      padding: 0 32px 28px 31px;
      position: relative;
      .threeinput {
        font-size: 12px;
        padding: 0;
        height: 13px;
      }
      .img-text {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        .img-div {
          width: 99px;
          height: 136px;
          background: #446583;
          display: flex;
          align-items: center;
          span {
            display: block;
            margin: 0 auto;
            width: 12px;
            color: #fff;
          }
        }
        .text-right {
          margin-left: 18px;
          background: #f6f5f5;
          padding: 6px 12px;
          flex: 1;
          box-sizing: border-box;
          input {
            background: #f6f5f5;
          }
          p {
            margin-bottom: 10px;
            height: 10px;
            background: #dee4e7;
            color: #999999;
          }
        }
      }
      .chiz {
        display: flex;
        align-items: top;
        justify-content: flex-start;
        margin-top: 15px;
        margin-bottom: 13px;
        span {
          display: inline-block;
          color: #ff9797;
          font-size: 12px;
          width: 60px;
        }
        input {
          color: #ff9797;
          flex: 1;
        }
      }
      .qianfa {
        font-size: 14px;
        display: flex;
        align-items: center;
        justify-content: flex-start;
        span {
          color: #999999;
          width: 70px;
        }
        a {
          font-size: 14px;
          flex: 1;
        }
        input {
          flex: 1;
        }
      }
      .qfdw {
        position: relative;
      }
      .zhang {
        position: absolute;
        right: -26px;
        top: -62px;
        width: 150px;
        height: 150px;
        z-index: 10000;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .anquan {
        margin-top: 17px;
        border-top: 1px solid #e9e9e9;
        padding-top: 10px;
        p {
          font-size: 12px;
          color: #999999;
        }
        ul {
          padding-left: 0;
          display: flex;
          align-items: center;
          justify-content: flex-start;
          li {
            width: 33%;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
            i {
              display: block;
            }
            i:first-child {
              width: 41px;
              height: 41px;
              background: url('../../../assets/gm.png') no-repeat center;
              background-size: 41px 41px;
            }
            i:last-child {
              width: 12px;
              height: 12px;
              background: url('../../../assets/xz.png') no-repeat center;
              background-size: 12px 12px;
            }
            p {
              line-height: 1;
              font-size: 12px;
              color: #999999;
              margin: 9px 0 3px;
            }
          }
        }
      }
    }
  }
}
.zjStyle-content-footer {
  margin-top: 13px;
  width: 335px;
  padding: 21px 16px 17px;
  background: #ffffff;
  box-shadow: 3px 3px 7px 3px rgba(8, 8, 8, 0.1);
  border-radius: 10px;
  font-size: 12px;
  &-qf:first-child {
    margin-bottom: 10px;
  }
  &-qf {
    display: flex;
    align-items: center;
    justify-content: flex-start;
  }
  span {
    line-height: 1;
    margin-right: 25px;
    color: #6e6e6e;
  }
  input {
    border: none;
    flex: 1;
    padding: 0;
    height: 14px;
  }
}
.zjzcj {
  line-height: 20px;
  margin-bottom: 0;
  color: #999999;
  padding-left: 27px;
}
.yx {
  color: #999;
  font-size: 12px;
  margin-left: 10px;
}
.bcbtn {
  width: 100px;
}
.bcbtn:first-child {
  margin-right: 20px;
}
.fz-span {
  display: block;
  height: 100%;
  width: 100%;
  border-bottom: 1px solid #eee;
}
.djsc {
  width: 100px;
}
.imgfather {
  width: 150px;
  height: 150px;
  padding: 10px;
  box-sizing: border-box;
  border: 1px solid #eee;
  position: relative;
  .imgUrl {
    width: 100%;
    height: 100%;
  }
  .imgzzc {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
  }
}

.card-span2 {
  width: 212px !important;
  overflow: hidden;
  display: block !important;
}

.codeModal {
  .codeImg {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    &-centent {
      width: 150px;
      height: 150px;
      margin-bottom: 20px;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
.aloneGiveCert {
  h3 {
    width: 20.83333333%;
    color: #999999;
    text-align: right;
    padding-right: 10px;
    font-size: 14px;
  }
}
</style>
<style lang="less">
.fzfrom .ant-form-item-label > label {
  color: #999999;
}
</style>
